﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UiScript : MonoBehaviour
{

	public bool isRoot = false;
	private string path = "";

	void Start()
	{
		if (this.isRoot == true)
		{
			this.path = this.name;
			Debug.Log("load ui " + this.path);
			UiManager.getInsance().add(this);
		}
		else {
			UiScript parent = this.getParentUiScript();
			if (parent != null && parent.getPath() != "")
			{
				this.path = parent.getPath() + "/" + this.name;
				Debug.Log("load ui " + this.path);
				UiManager.getInsance().add(this);
			}
			else {
				Debug.LogError("error: load ui " + this.name + " error");
			}
		}
		
	}

	void Update()
	{

	}

	void OnDestroy()
	{
		if (this.path != "") {
			UiManager.getInsance().remove(this);
			Debug.Log("info: destory ui " + this.path);
		}
	}

	//获取父界面的组件
	private UiScript getParentUiScript()
	{
		if (this.isRoot == true)
		{
			return null;
		}

		return this.transform.parent.GetComponent<UiScript>();
	}

	//获取当前ui的路径
	public string getPath()
	{
		return this.path;
	}
}
